package com.example.meituan.activity.user;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.widget.Toolbar;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

import com.example.meituan.MainActivity;
import com.example.meituan.R;
import com.example.meituan.dao.AdminDao;
import com.example.meituan.dao.JiaMi;
import com.example.meituan.until.FileImgUntil;

public class RegisterUserActivity extends AppCompatActivity {
    private ActivityResultLauncher<String> getContentLauncher;

    private Uri url;
    //打开文件选择器
    private void openGallery(View v){
        getContentLauncher.launch("image/*");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_register_user);


        //实现一个返回的功能
        Toolbar toolbar=findViewById(R.id.register_user_toolbar);
        setSupportActionBar(toolbar);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
                                                 @Override
                                                 public void onClick(View v) {
                                                     onBackPressed();//返回功能

                                                 }
                                             }
        );
        //获取头像
        Drawable defaultDrawable= ContextCompat.getDrawable(this,R.drawable.photo);

        EditText idText=findViewById(R.id.register_user_id);
        EditText pwdText=findViewById(R.id.register_user_pwd);
        EditText pwdTryText=findViewById(R.id.register_user_pwd_try);
        EditText nameText=findViewById(R.id.register_user_name);
        EditText phoneText=findViewById(R.id.register_user_phone);
        EditText shenText=findViewById(R.id.register_user_shen);
        ImageView img=findViewById(R.id.register_user_photo);
        TextView reg=findViewById(R.id.register_user_reg);


        getContentLauncher=registerForActivityResult(new ActivityResultContracts.GetContent(), new ActivityResultCallback<Uri>() {
                    @Override
                    public void onActivityResult(Uri result) {
                        if(result!=null)
                        {
                            img.setImageURI(result);
                            url =result;
                        }else{
                            Toast.makeText(RegisterUserActivity.this,"未选择头像",Toast.LENGTH_SHORT).show();
                        }
                    }
                }

        );

        img.setOnClickListener(new View.OnClickListener() {
                                   @Override
                                   public void onClick(View v) {
                                       //打开手机相册,实现上传图片
                                       openGallery(v);

                                   }
                               }

        );

        reg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean isValid = true;
                StringBuilder errorMessage = new StringBuilder();

                // 获取输入内容
                Drawable drawable = img.getDrawable();
                String id = idText.getText().toString().trim();
                String pwd = pwdText.getText().toString().trim();
                String pwdTry = pwdTryText.getText().toString().trim();
                String name = nameText.getText().toString().trim();
                String phone = phoneText.getText().toString().trim();
                String shen = shenText.getText().toString().trim();

                // 验证图片是否为空或默认
                if (drawable instanceof BitmapDrawable) {
                    Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
                    if (defaultDrawable != null && bitmap.sameAs(((BitmapDrawable) defaultDrawable).getBitmap())) {
                        errorMessage.append("图片不能为空\n");
                        isValid = false;
                    }
                }

                // 验证其他字段
                if (id.isEmpty()) {
                    errorMessage.append("账号不能为空\n");
                    isValid = false;
                }
                if (pwd.isEmpty()) {
                    errorMessage.append("密码不能为空\n");
                    isValid = false;
                }
                if (pwdTry.isEmpty()) {
                    errorMessage.append("请再次输入密码\n");
                    isValid = false;
                }
                if (!pwd.equals(pwdTry)) {
                    errorMessage.append("两次输入密码不一致\n");
                    isValid = false;
                }
                if (name.isEmpty()) {
                    errorMessage.append("请输入昵称\n");
                    isValid = false;
                }
                if (phone.isEmpty()) {
                    errorMessage.append("请输入手机号\n");
                    isValid = false;
                } else if (phone.length() != 11 || !android.util.Patterns.PHONE.matcher(phone).matches()) {
                    errorMessage.append("手机号格式不正确\n");
                    isValid = false;
                }
                if (shen.isEmpty()) {
                    errorMessage.append("请输入身份证号\n");
                    isValid = false;
                } else if (shen.length() != 18) {
                    errorMessage.append("身份证号格式不正确\n");
                    isValid = false;
                }

                // 如果有错误信息，则显示出来
                if (!isValid) {
                    Toast.makeText(RegisterUserActivity.this, errorMessage.toString(), Toast.LENGTH_SHORT).show();
                } else {
                    // 所有验证通过后进行加密和保存操作
                    pwd = JiaMi.encryptPassword(pwd);
                    String path = FileImgUntil.getImgPath(); // 获取存储图片的路径
                    FileImgUntil.saveImageBitmapToFile(url, RegisterUserActivity.this, path); // 保存图片

                    int result = AdminDao.saveUser(id, pwd, name, phone, shen, path);
                    if (result == 1) {
                        Toast.makeText(RegisterUserActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                        Intent intent=new Intent(RegisterUserActivity.this, MainActivity.class);
                        startActivity(intent);
                    } else {
                        Toast.makeText(RegisterUserActivity.this, "注册失败", Toast.LENGTH_SHORT).show();
                    }
                }

            }
        });


    }
}